package com.stack.auto_parts.controller;

import com.stack.auto_parts.common.ApiResult;
import com.stack.auto_parts.entity.User;
import com.stack.auto_parts.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Title: UserController
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.controller.register
 * @Date 2024/7/8 18:27
 * @description: 注册
 */

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ApiResult<Void> register(@RequestBody User user){

        String result = userService.register(user);
        if(result != null)
            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

    /**
     * 删除指定User
     * @param
     * @return
     */
    @DeleteMapping("/delete")
    public ApiResult<Void> delete(String uuid){
        String result = userService.deleteUser(uuid);
        if(result != null)

            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

    @GetMapping("/selectAll")
    public ApiResult<List<User>> selectAll(){
        List<User> userList = userService.selectAll();
        return ApiResult.success(userList);
    }

    @GetMapping("/selectBycondition")
    public ApiResult<List<User>> listBycondition(String userName, String phone , String role){
        if(Objects.equals(userName, ""))
            userName = null;
        if(Objects.equals(phone, ""))
            phone =null;
        if(Objects.equals(role, ""))
            role =null;
        System.out.println(userName+phone+role);
        List<User> userList = userService.selectBycondition(userName,phone, role);
        return ApiResult.success(userList);
    }

    @PostMapping("/updateUser")
    public ApiResult<Void> updateUser(@RequestBody User user){
        String result = userService.updateUser(user);
        if(result != null)
            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

}
